%{
#include <stdio.h>
#include <ctype.h>
int yylex(void);
void yyerror(char const* s);
char err[1024] = {'\0'};
%}
%token NUM 
%token ERR
%%
jin:/* empty */
	| jin line
;
line: '\n'
	| my_exp '\n' { printf("my_exp = %d\n",$1); }
	| my_err '\n' { printf("my_err = %s\n",err); }
;
my_exp: NUM { $$ = $1;printf("num = %d\n",$1);}
	| my_exp '+' my_exp { $$ = $1 + $3; }
;
my_err: ERR
;
%%
int main(void)
{
	return yyparse();
}
void yyerror(char const* s)
{
	printf("%s\n",s);
}
// 处理字符串
int yylex(void)
{	
	char c;
	c = getchar();
	// printf("c = %c\n",c);
	if( isdigit(c) )
	{
		ungetc(c,stdin);
		scanf("%d",&yylval);
		return NUM;
	}
	// 不在规则中的字符要原样返回
	if(c != '+' && c != '\n')
	{
		ungetc(c,stdin);
		scanf("%d",&yylval);
		fgets(err,sizeof(err),stdin);
		ungetc('\n',stdin);
		return ERR;
	}
	// printf("unknown = %c\n",c);
	return c;
}
